/**
 * Sends the five9 tasks over to Five9_CallDispositions.chkCallDisposition
 */
trigger TaskAfterInsert_OnCallDisposition on Task (after insert,after update) {
	List<Task> five9_Tasks = new List<Task>();
	string MobilePhone;
	string WorkPhone;
	string HomePhone;
	string MarketingPhone;
	string OtherPhone;
	string OtherPhone2;
	for(Task tempTask : Trigger.new) {
		if(tempTask.Five9__Five9Campaign__c != null) {
			five9_Tasks.add(tempTask);
		}
	}
	//Five9_CallDispositions.chkCallDisposition(five9Tasks);
	
	map<string,Five9_Campaign_Management__c> dispositions = new map<string,Five9_Campaign_Management__c>();
    for(Five9_Campaign_Management__c tempFive9disp : Five9_Campaign_Management__c.getall().values())
    {
    	dispositions.put(tempFive9disp.Five9_Disposition__c,tempFive9disp);            
    }
        
    Set<Id> leadIdSet = new Set<Id>();
    for(Task tk : five9_Tasks) {
        String whoIdStr = tk.whoId;
    	if(whoIdStr != null && whoIdStr.startswith('00Q'))
       		leadIdSet.add(tk.whoId);	                
    }
    
    if(leadIdSet.size() > 0) {    	
   		Map<Id, Lead> leadMap = new Map<Id, Lead>([Select l.Id, l.OwnerId, l.Status_Detail__c, l.Status, l.DoNotCall, l.Hot_Transfer__c,
   			    					l.Work_Phone__c, l.Work_Phone_Unmasked__c, l.Work_Phone_Preferences__c, 
   			    					l.Other_Phone__c, l.Other_Phone_Unmasked__c, l.Other_Phone_Preferences__c, 
				       			    l.Other_Phone_2__c, l.Other_Phone_2_Unmasked__c, l.Other_Phone_2_Preferences__c, 
				       			    l.Mobile_Phone_Unmasked__c, l.Mobile_Phone_Preferences__c, l.MobilePhone, 
				       			    l.Marketing_Phone__c, l.Marketing_Phone_Unmasked__c, l.Marketing_Phone_Preferences__c, 
				       			    l.Home_Phone__c, l.Home_Phone_Unmasked__c, l.Home_Phone_Preferences__c 
			       		 		From Lead l
			       		 		where Id IN: leadIdSet]);
   		List<Lead> updateLeads = new List<Lead>();
   		system.debug('9874');
   		for(Task tempTask : five9_Tasks) {
   			system.debug('9875');
   			system.debug(tempTask.CallDisposition);
   			for(string sss : dispositions.Keyset()) {
   				system.debug(sss);
   			}
    		if(dispositions.containsKey(tempTask.CallDisposition)) {
    			system.debug('9872');
    			Five9_Campaign_Management__c five9CamMan = dispositions.get(tempTask.CallDisposition);
    			if(leadMap.containsKey(tempTask.whoId)) {
    				system.debug('9873');
    				Lead editLead = leadMap.get(tempTask.whoId);
    				if(five9CamMan.DNC_All_Numbers__c) {
    					editLead.DoNotCall = true;
    				}
    				if(five9CamMan.Hot_Transfer__c) {
    					editLead.Hot_Transfer__c = true;
    				}
    				if(five9CamMan.Single_Number_Update__c != null && five9CamMan.Single_Number_Update__c != '') {
    					//Converting string to the format of a phone number and phone number to string.
    					string s = tempTask.Five9__Five9DNIS__c;
    					
    					if( tempTask.Five9__Five9DNIS__c !=null)
    						s = '(' + s.substring(0, 3) + ') ' + s.substring(3, 6) + '-' + s.substring(6);
    					else
    						system.debug('Enter Five9DNIS number to do update');
    					
    					system.debug(editLead.Home_Phone__c);
    					MobilePhone = editLead.MobilePhone;
						WorkPhone = editLead.Work_Phone__c;
						HomePhone = editLead.Home_Phone__c;
						MarketingPhone = editLead.Marketing_Phone__c;
						OtherPhone = editLead.Other_Phone__c;
						OtherPhone2 = editLead.Other_Phone_2__c;
					    					
    					if(s == (string)editLead.MobilePhone || tempTask.Five9__Five9DNIS__c == (string)editLead.MobilePhone) {
    						editLead.Mobile_Phone_Preferences__c = five9CamMan.Single_Number_Update__c;
    						MobilePhone = null;
    					}
    						
    					if(s == (string)editLead.Work_Phone__c|| tempTask.Five9__Five9DNIS__c == (string)editLead.Work_Phone__c) {
    						editLead.Work_Phone_Preferences__c = five9CamMan.Single_Number_Update__c;
    						WorkPhone = null;
    					}    						
    					 	
    					if(s == (string)editLead.Marketing_Phone__c|| tempTask.Five9__Five9DNIS__c == (string)editLead.Marketing_Phone__c) {
    						editLead.Marketing_Phone_Preferences__c = five9CamMan.Single_Number_Update__c;
    						MarketingPhone = null;
    					}
    						
    					if(s == (string)editLead.Home_Phone__c|| tempTask.Five9__Five9DNIS__c == (string)editLead.Home_Phone__c) {
    						editLead.Home_Phone_Preferences__c = five9CamMan.Single_Number_Update__c;
    						HomePhone = null;
    					}
    						
    					if(s == (string)editLead.Other_Phone__c|| tempTask.Five9__Five9DNIS__c == (string)editLead.Other_Phone__c) {
    						editLead.Other_Phone_Preferences__c = five9CamMan.Single_Number_Update__c;
    						OtherPhone = null;
    					}
    						
    					if(s == (string)editLead.Other_Phone_2__c|| tempTask.Five9__Five9DNIS__c == (string)editLead.Other_Phone_2__c) {
    						editLead.Other_Phone_2_Preferences__c = five9CamMan.Single_Number_Update__c;
    						OtherPhone2 = null;
    					}    						
    				}
    				system.debug('9870');
    				if(five9CamMan.Take_Final_Disposition_Actions__c && !five9CamMan.When_No_Number_Present__c) {
    					if(five9CamMan.Change_Record_Owner__c != null )
    						editLead.OwnerId = five9CamMan.Change_Record_Owner__c;
    					if(five9CamMan.Status_Update__c != null)
    						editLead.Status = five9CamMan.Status_Update__c;
    					if(five9CamMan.Status_Detail_Update__c != null)
    						editLead.Status_Detail__c = five9CamMan.Status_Detail_Update__c;
    				}
    				
    				//When No number present had to chk with john
    				if(five9CamMan.Take_Final_Disposition_Actions__c &&
    				   five9CamMan.When_No_Number_Present__c &&
    				     (MobilePhone == null && WorkPhone == null && MarketingPhone == null 
    				      && HomePhone == null && OtherPhone == null && OtherPhone2 == null)) {
    					system.debug('9876');
    					if(five9CamMan.Change_Record_Owner__c != null)
    						editLead.OwnerId = five9CamMan.Change_Record_Owner__c;
    					if(five9CamMan.Status_Update__c != null)
    						editLead.Status = five9CamMan.Status_Update__c;
    					if(five9CamMan.Status_Detail_Update__c!= null)
    						editLead.Status_Detail__c = five9CamMan.Status_Detail_Update__c;
    				}
    				updateLeads.add(editLead);    					
    			}	
    		}
    	}
    	update updateLeads;
    }        
}